<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" size="small" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="企业名称" prop="companyName"
                :rules="[
                  {required: true, message:'企业名称不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.companyName" placeholder="请填写企业名称" disabled></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="姓名" prop="userName"
                :rules="[
                  {required: true, message:'姓名不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.userName" placeholder="请填写姓名" disabled ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="电话" prop="phone"
                :rules="[
                  {required: true, message:'电话不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.phone" placeholder="请填写电话" disabled></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="客户备注" prop="comRemarks"
                :rules="[
                 ]">
              <el-input v-model="inputForm.comRemarks" placeholder="请填写客户备注" disabled></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="是否回访" prop="isVisit"
                :rules="[
                  {required: true, message:'是否回访不能为空', trigger:'blur'}
                 ]">
                <el-select v-model="inputForm.isVisit" placeholder="请选择"  style="width: 100%;" disabled>
                          <el-option
                            v-for="item in $dictUtils.getDictList('yes_no')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="回访人" prop="visituser.id"
                :rules="[
                 ]">
                <user-select :limit='1' :value="inputForm.visituser.id" @getValue='(value) => {inputForm.visituser.id=value}'></user-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="回访时间" prop="visittime"
                :rules="[
                 ]">
                <el-date-picker disabled
                      style="width: 100%;"
                      v-model="inputForm.visittime"
                      type="datetime"
                      value-format="yyyy-MM-dd HH:mm:ss"
                      placeholder="选择日期时间">
                    </el-date-picker>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="联系企业" prop="connectcom.id"
                :rules="[
                 ]">
          <SelectTree disabled
                      ref="connectcom"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'children'    // 子级字段名
                        }"

                      url="/sys/office/treeData?type=2"
                      :value="inputForm.connectcom.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.connectcom.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import UserSelect from '@/components/userSelect'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          companyName: '',
          userName: '',
          phone: '',
          comRemarks: '',
          isVisit: '',
          visituser: {
            id: ''
          },
          visittime: '',
          connectcom: {
            id: ''
          },
          remarks: ''
        }
      }
    },
    components: {
      UserSelect,
      SelectTree
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建微信联系企业`
        } else if (method === 'edit') {
          this.title = '修改微信联系企业'
        } else if (method === 'view') {
          this.title = '查看微信联系企业'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/sys/wechatcompany/wechatCompany/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.wechatCompany)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/sys/wechatcompany/wechatCompany/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>